from __future__ import division
import seaborn as sns
import numpy as np
import pandas as pd
sns.set()


def main():
    # 画图：温度与传感器关系图
    dataset = pd.read_excel('../data/drawDate.xlsx')
    # 要分析的数据
    x_index = dataset.iloc[0, :360].values
    X = dataset.iloc[1:33, :360].values

    print(np.array(X).shape)
    print(X)
    mat= X[0]
    print("初始数据：mat")
    print(mat)
    print(DenoisMat(mat))


# 获得矩阵的字段数量
def width(lst):
    i = 0
    for j in lst[0]:
        i += 1
    return i


# 得到每个字段的平均值
def GetAverage(mat):
    n = len(mat)
    m = width(mat)
    num = [0] * m
    for i in range(0, m):
        for j in mat:
            num[i] += j[i]
        num[i] = num[i] / n
    return num


# 获得每个字段的标准差
def GetVar(average, mat):
    ListMat = []
    for i in mat:
        ListMat.append(list(map(lambda x: x[0] - x[1], zip(average, i))))

    n = len(ListMat)
    m = width(ListMat)
    num = [0] * m
    for j in range(0, m):
        for i in ListMat:
            num[j] += i[j] * i[j]
        num[j] /= n
    return num


# 获得每个字段的标准差
def GetStandardDeviation(mat):
    return list(map(lambda x: x ** 0.5, mat))


# 对数据集去噪声
def DenoisMat(mat):
    average = GetAverage(mat)
    variance = GetVar(average, mat)
    standardDeviation = GetStandardDeviation(variance)
    section = list(map(lambda x: x[0] + 3 * x[1], zip(average, standardDeviation)))
    n = len(mat)
    m = width(mat)
    num = [0] * m
    denoisMat = []
    noDenoisMat = []
    for i in mat:
        for j in range(0, m):
            if i[j] > section[j]:
                denoisMat.append(i)
                break
            if j == (m - 1):
                noDenoisMat.append(i)
    print("去除完噪声的数据：")
    print(noDenoisMat)
    print("噪声数据：")
    return denoisMat


if __name__ == '__main__':
    main()

